Skip to content

fix: accumulate all text blocks in Anthropic non-streaming response - #9786

Open
AmirF194 wants to merge 1 commit into
AstrBotDevs:masterfrom
AmirF194:fix/anthropic-multi-text-block-overwrite
Open

fix: accumulate all text blocks in Anthropic non-streaming response#9786
AmirF194 wants to merge 1 commit into
AstrBotDevs:masterfrom
AmirF194:fix/anthropic-multi-text-block-overwrite

Conversation

@AmirF194

@AmirF194 AmirF194 commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

ProviderAnthropic._query (the non-streaming path in anthropic_source.py) sets llm_response.completion_text inside a loop over completion.content, once per text content block, instead of appending. When a Message carries more than one text block, only the last one survives; every earlier text block is silently dropped from the reply and from conversation history. This can happen with the Citations API and with extended-thinking flows, both of which can split a response into multiple text blocks. The streaming sibling, _query_stream, already accumulates correctly with final_text += event.delta.text.

Modifications / 改动点

Collect each text block into a list during the loop and join them once after the loop, instead of overwriting completion_text per block. thinking and tool_use handling are unchanged.

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

Added test_query_accumulates_multiple_text_blocks, which feeds ProviderAnthropic._query a fake Message with two text blocks. It fails on unmodified master (AssertionError, only the second block's text survives) and passes with this fix. The full tests/test_anthropic_kimi_code_provider.py file: 28 passed, 1 pre-existing failure unrelated to this change (test_create_http_client_uses_anthropic_httpx_module, an SDK version drift already being fixed in open PR #9769). ruff format --check and ruff check are clean on both changed files.

Not verified: a live call to the provider's API. This was reproduced and tested against the SDK's Message/content-block shape directly, not against a real multi-block response from the API.


Checklist / 检查清单

  • If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了"验证步骤"和"运行截图"

  • I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Accumulate every text block returned by Anthropic’s non-streaming API so complete responses and conversation history are preserved.

Bug Fixes:

  • Preserve all text content blocks in Anthropic non-streaming responses instead of returning only the final block.

Tests:

  • Add coverage verifying that multiple Anthropic text blocks are concatenated in the response.

The non-streaming _query loop set llm_response.completion_text on every
text content block instead of appending to it, so only the last text
block in a multi-block Message.content survived; earlier blocks were
silently dropped. The streaming path already accumulates correctly
with final_text +=. A Message can carry more than one text block (the
Citations API and extended-thinking flows both produce this shape).

Adds a regression test that fails on the old overwrite behavior and
passes with the fix.
@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. labels Aug 23, 2026

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Approved.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant